package com.kucai.yifaqapp.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kucai.yifaqapp.R;
import com.kucai.yifaqapp.global.HttpAdress;
import com.kucai.yifaqapp.global.Info;
import com.kucai.yifaqapp.model.CustomObserver;
import com.kucai.yifaqapp.model.network.HttpJsonUtils;
import com.kucai.yifaqapp.model.network.entity.res.ResMoney;
import com.kucai.yifaqapp.ui.fragment.RecommentFragmentfour;
import com.kucai.yifaqapp.ui.fragment.RecommentFragmentone;
import com.kucai.yifaqapp.ui.fragment.RecommentFragmentthree;
import com.kucai.yifaqapp.ui.fragment.RecommentFragmenttwo;

import butterknife.BindView;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by admin on 2018/12/20.
 */

public class RecommendActivity extends BaseActivitySimple {
    @BindView(R.id.skinbg)
    LinearLayout skinbg;
    @BindView(R.id.user_name)
    TextView userName;
    @BindView(R.id.money_tv)
    TextView moneyTv;
    @BindView(R.id.reletive1)
    RelativeLayout reletive1;
    @BindView(R.id.reletive2)
    RelativeLayout reletive2;
    @BindView(R.id.reletive3)
    RelativeLayout reletive3;
    @BindView(R.id.reletive4)
    RelativeLayout reletive4;
    @BindView(R.id.text1)
    TextView text1;
    @BindView(R.id.text2)
    TextView text2;
    @BindView(R.id.text3)
    TextView text3;
    @BindView(R.id.text4)
    TextView text4;
    @BindView(R.id.view1)
    View view1;
    @BindView(R.id.view2)
    View view2;
    @BindView(R.id.view3)
    View view3;
    @BindView(R.id.view4)
    View view4;
    @BindView(R.id.refreshyue)
    ImageView refreshyue;
    @BindView(R.id.title_left_tv)
    TextView titleLeftTv;
    @BindView(R.id.headerTitle)
    TextView headerTitle;
    @BindView(R.id.content)
    FrameLayout content;

    private Fragment RecommentFragment1 = new RecommentFragmentone();
    private Fragment RecommentFragment2 = new RecommentFragmenttwo();
    private Fragment RecommentFragment3 = new RecommentFragmentthree();
    private Fragment RecommentFragment4 = new RecommentFragmentfour();

    @Override
    protected int setContentView() {
        return R.layout.recommend_layout;
    }

    @Override
    protected void ChangeSkin() {
        switch (SkinId) {
            case 17771:
                skinbg.setBackgroundResource(R.mipmap.skin_bg1);
                break;
            case 17772:
                skinbg.setBackgroundResource(R.mipmap.skin_bg2);
                break;
            case 17773:
                skinbg.setBackgroundResource(R.mipmap.skin_bg3);
                break;
            default:
                skinbg.setBackgroundResource(R.mipmap.skin_bg1);
        }
    }

    protected void onResume() {
        super.onResume();
        if (Info.user != null) {
            if (Info.user.userName != null) {
                userName.setText(Info.user.userName + "");
            }
            if (Info.user.money != null) {
                moneyTv.setText("用户余额：" + Info.user.money);
            } else {
                moneyTv.setText("用户余额：0.00");
            }
        }
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        titleLeftTv.setText("返回");
        headerTitle.setText("我的推荐");
        titleLeftTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        refreshyue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getUserMoney();
            }
        });
        initFragment();
        initEvents();
    }

    public void getUserMoney() {
        HttpJsonUtils.getHttpApiInstance()
                .getmoney(HttpAdress.getuseAdress() + "api/m/user/getMoney",Info.user.token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<ResMoney>(this) {
                    @Override
                    protected void onCustomNext(ResMoney resOods) {
                        if (resOods.code == 0) {
                            moneyTv.setText("用户余额：" + resOods.data.balance);
                        }
                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {
                    }
                });
    }

    private void initEvents() {
        reletive1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeFragmemt("1");

            }
        });
        reletive2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeFragmemt("2");
            }
        });
        reletive3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeFragmemt("3");
            }
        });
        reletive4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeFragmemt("4");
            }
        });
    }

    private void initFragment() {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        if (!RecommentFragment1.isAdded()) {
            ft.add(R.id.content, RecommentFragment1, "1");
        }
        if (!RecommentFragment2.isAdded()) {
            ft.add(R.id.content, RecommentFragment2, "2");
        }
        if (!RecommentFragment3.isAdded()) {
            ft.add(R.id.content, RecommentFragment3, "3");
        }
        if (!RecommentFragment4.isAdded()) {
            ft.add(R.id.content, RecommentFragment4, "4");
        }
        ft.commit();
        changeFragmemt("1");
    }

    public void changeFragmemt(String tag) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        if (tag.equals("1")) {
            ft.show(RecommentFragment1);
            ft.hide(RecommentFragment2);
            ft.hide(RecommentFragment3);
            ft.hide(RecommentFragment4);
            text1.setTextColor(getResources().getColor(R.color.blue));
            text2.setTextColor(getResources().getColor(R.color.white));
            text3.setTextColor(getResources().getColor(R.color.white));
            text4.setTextColor(getResources().getColor(R.color.white));
            view1.setVisibility(View.VISIBLE);
            view2.setVisibility(View.GONE);
            view3.setVisibility(View.GONE);
            view4.setVisibility(View.GONE);
        } else if (tag.equals("2")) {
            ft.show(RecommentFragment2);
            ft.hide(RecommentFragment1);
            ft.hide(RecommentFragment3);
            ft.hide(RecommentFragment4);
            text2.setTextColor(getResources().getColor(R.color.blue));
            text1.setTextColor(getResources().getColor(R.color.white));
            text3.setTextColor(getResources().getColor(R.color.white));
            text4.setTextColor(getResources().getColor(R.color.white));
            view2.setVisibility(View.VISIBLE);
            view1.setVisibility(View.GONE);
            view3.setVisibility(View.GONE);
            view4.setVisibility(View.GONE);
        } else if (tag.equals("3")) {
            ft.show(RecommentFragment3);
            ft.hide(RecommentFragment2);
            ft.hide(RecommentFragment1);
            ft.hide(RecommentFragment4);
            text3.setTextColor(getResources().getColor(R.color.blue));
            text2.setTextColor(getResources().getColor(R.color.white));
            text1.setTextColor(getResources().getColor(R.color.white));
            text4.setTextColor(getResources().getColor(R.color.white));
            view3.setVisibility(View.VISIBLE);
            view2.setVisibility(View.GONE);
            view1.setVisibility(View.GONE);
            view4.setVisibility(View.GONE);
        } else if (tag.equals("4")) {
            ft.show(RecommentFragment4);
            ft.hide(RecommentFragment2);
            ft.hide(RecommentFragment3);
            ft.hide(RecommentFragment1);
            text4.setTextColor(getResources().getColor(R.color.blue));
            text2.setTextColor(getResources().getColor(R.color.white));
            text3.setTextColor(getResources().getColor(R.color.white));
            text1.setTextColor(getResources().getColor(R.color.white));
            view4.setVisibility(View.VISIBLE);
            view2.setVisibility(View.GONE);
            view3.setVisibility(View.GONE);
            view1.setVisibility(View.GONE);
        }
        ft.commit();
    }
}
